//
//  TableViewSettingsViewController.m
//  TableViewSettings
//
//  Created by easystudio on 11-10-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TableViewSettingsViewController.h"

@implementation TableViewSettingsViewController
@synthesize settingsArray;
@synthesize datePicker;
@synthesize settingTableView;
@synthesize sliderView;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	NSDictionary *d1 = [NSDictionary dictionaryWithObjectsAndKeys:
						@"启动翻页声音", @"title",
						@"", @"subTitle",
						@"BOOL", @"type",
						nil];
	NSDictionary *d2 = [NSDictionary dictionaryWithObjectsAndKeys:
						@"允许横屏", @"title",
						@"", @"subTitle",
						@"BOOL", @"type",
						nil];
	NSDictionary *d3 = [NSDictionary dictionaryWithObjectsAndKeys:
						@"同步时间", @"title",
						@"2011-11-09", @"subTitle",
						@"CHECK", @"type",
						nil];
	NSDictionary* sl=[NSDictionary dictionaryWithObjectsAndKeys:
					  @"屏幕亮度", @"title",
					  @"0%", @"subTitle",
					  @"CHECK", @"type",
					  nil];
	NSArray *array1 = [NSArray arrayWithObjects:
					   d1, d2, d3, sl,nil];
	
	NSDictionary *d4 = [NSDictionary dictionaryWithObjectsAndKeys:
						@"主题", @"title",
						@"经典主题", @"subTitle",
						@"CHECK", @"type",
						nil];
	NSDictionary *d5 = [NSDictionary dictionaryWithObjectsAndKeys:
						@"晃动切换主题", @"title",
						@"", @"subTitle",
						@"BOOL", @"type",
						nil];
	NSArray *array2 = [NSArray arrayWithObjects:
					   d4, d5, nil];
	
	
	NSArray *allArray = [NSArray arrayWithObjects:
						 [NSDictionary dictionaryWithObjectsAndKeys:
						  @"通用设置", @"title",
						  array1, @"value",nil],
						 [NSDictionary dictionaryWithObjectsAndKeys:
						  @"主题", @"title",
						  array2, @"value", nil],
						 nil
						 ];
	self.settingsArray = allArray;
	
	NSString* tmp=@"";
	if (tmp) {
		NSLog(@"tmp is not nil");
	}
	else {
		NSLog(@"tmp is nil");
	}

	
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.settingsArray=nil;
	self.datePicker=nil;
	self.settingTableView=nil;
	self.sliderView=nil;
}


- (void)dealloc {
	[settingsArray release];
	[datePicker release];
	[settingTableView release];
	[sliderView release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSDictionary* dic =[self.settingsArray objectAtIndex:section];
	NSArray* array=[dic objectForKey:@"value"];
	return [array count];
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	NSDictionary* dic=[self.settingsArray objectAtIndex:section];
	return [dic objectForKey:@"titile"];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger row=[indexPath row];
	NSInteger section=[indexPath section];
	NSDictionary* dic=[self.settingsArray objectAtIndex:section];
	NSArray* array=[dic objectForKey:@"value"];
	
	NSDictionary* item=[array objectAtIndex:row];
	NSString* type=[item objectForKey:@"type"];
	NSString* title=[item objectForKey:@"title"];
	NSString* subTitle=[item objectForKey:@"subTitle"];
	NSString* name;
	if([type isEqual:@"BOOL"])
	{
		name=@"BoolCell";
	}
	else {
		name=@"CheckCell";
	}
	
	UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:name];
	if (nil==cell) {
		cell=[[[NSBundle mainBundle]loadNibNamed:name owner:self options:nil] lastObject];
	}
	
	UILabel* titleLabel=(UILabel*)[cell viewWithTag:101];
	UILabel* subTitleLabel=(UILabel*)[cell viewWithTag:102];
	
	titleLabel.text=title;

	if (subTitleLabel)
	{
		subTitleLabel.text=subTitle;
		cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
		cell.selectionStyle=UITableViewCellSelectionStyleBlue;
	}
	else 
	{
		cell.selectionStyle=UITableViewCellSelectionStyleNone;
	}
	
	return cell;
	

	
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell* cell=[tableView cellForRowAtIndexPath:indexPath];
	UILabel* titleLabel=(UILabel*)[cell viewWithTag:101];
	if([titleLabel.text isEqualToString:@"同步时间"])
	{
		[self showDatePicker];
	}
	else if([titleLabel.text isEqualToString:@"屏幕亮度"])
	{
		[self showSliderView];
	}
}

#pragma mark -
#pragma mark Date Picker
-(void)showDatePicker
{
	NSLog(@"showDatePicker");
	if (self.datePicker.superview) {
		NSIndexPath* indexPath=[self.settingTableView indexPathForSelectedRow];
		[self.datePicker removeFromSuperview];
		[self.settingTableView deselectRowAtIndexPath:indexPath animated:YES];
	}
	else{
		
		UIDatePicker* dp=[[UIDatePicker alloc] init];
		self.datePicker=dp;
		[dp release];
		
		[self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
		
		CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
		CGSize pickerSize = [dp sizeThatFits:CGSizeZero];
		
		datePicker.frame=CGRectMake(0, screenRect.origin.y+(screenRect.size.height-pickerSize.height), pickerSize.width, pickerSize.height);
		
		[self.view addSubview:self.datePicker];
	}
	
}

- (IBAction) dateChanged:(id)sender
{
	NSIndexPath *indexPath = [self.settingTableView indexPathForSelectedRow];
	UITableViewCell *cell = [self.settingTableView cellForRowAtIndexPath:indexPath];
	UILabel *label = (UILabel *)[cell viewWithTag:102];
	
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateStyle:NSDateFormatterShortStyle];
	[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
	
	label.text = [dateFormatter stringFromDate:self.datePicker.date];
	[dateFormatter release];	
		
}

#pragma mark -
#pragma mark Slider View

-(void)showSliderView
{
	if (self.sliderView.superview) {
		[self.sliderView removeFromSuperview];
		
		NSIndexPath* indexPath=[self.settingTableView indexPathForSelectedRow];
		[self.settingTableView deselectRowAtIndexPath:indexPath animated:YES];
	}
	else {
		CGRect screenRect=[[UIScreen mainScreen] applicationFrame];
		CGSize sliderViewSize=[self.sliderView sizeThatFits:CGSizeZero];
		self.sliderView.frame=CGRectMake(0, screenRect.origin.y+screenRect.size.height-sliderViewSize.height, 
										 sliderViewSize.width, sliderViewSize.height);
		[self.view addSubview:self.sliderView];
	}

}

-(IBAction)sliderValueChanged:(id)sender
{
	UISlider* slider=(UISlider*)sender;
	NSInteger val=[slider value];
	NSString* str=[NSString stringWithFormat:@"%d%%", val];
	
	NSIndexPath* indexPath=[self.settingTableView indexPathForSelectedRow];
	UITableViewCell* cell=[self.settingTableView cellForRowAtIndexPath:indexPath];
	
	UILabel* label=(UILabel*)[cell viewWithTag:102];
	
	label.text=str;
}
@end
